package chapter19;

import java.util.Iterator;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/4/3 11:26
 */
class Mail {
    static long counter = 0;
    GeneralDelivery generalDelivery;
    Scannability scannability;
    Readability readability;
    Address address;
    ReturnAddress returnAddress;
    long id = counter++;

    @Override
    public String toString() {
        return "Mail " + id;
    }

    public String details() {
        StringBuilder builder = new StringBuilder(toString());
        builder.append(",General Delivery:" + generalDelivery);
        builder.append(",Address Scanability:" + scannability);
        builder.append(",Address Readability:" + readability);
        builder.append(",Address address:" + address);
        builder.append(",Return address:" + returnAddress);

        return builder.toString();
    }

    enum GeneralDelivery {YES, NO1, NO2, NO3, NO4, NO5}

    enum Scannability {UNSCANNABLE, YES1, YES2, YES3, YES4}

    enum Readability {ILLEGIBLE, YES1, YES2, YES3, YES4}

    enum Address {INCORRECT, OK1, OK2, OK3, OK4, OK5, OK6}

    enum ReturnAddress {MISSING, OK1, OK2, OK3, OK4, OK5}
}

/**
 * @author lzy
 */
public class RandomMail {
    public static Mail randomMail() {
        Mail mail = new Mail();
        mail.generalDelivery = Enums.random(Mail.GeneralDelivery.class);
        mail.scannability = Enums.random(Mail.Scannability.class);
        mail.readability = Enums.random(Mail.Readability.class);
        mail.address = Enums.random(Mail.Address.class);
        mail.returnAddress = Enums.random(Mail.ReturnAddress.class);
        return mail;
    }

    public static Iterable<Mail> generator(final int count) {
        return new Iterable<Mail>() {
            int n = count;

            @Override
            public Iterator<Mail> iterator() {
                return new Iterator<Mail>() {
                    @Override
                    public boolean hasNext() {
                        return n-- > 0;
                    }

                    @Override
                    public Mail next() {
                        return randomMail();
                    }
                };
            }
        };
    }
}
